package com.jinduplan.planmanager.utils;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;

import androidx.recyclerview.widget.RecyclerView;

import com.cxz.kotlin.baselibs.adapter.ViewHolder;
import com.cxz.kotlin.baselibs.interfaces.OnItemClick;
import com.jinduplan.planmanager.bean.DataProjectBean;
import com.jinduplan.planmanager.bean.ProjectLabour;
import com.jinduplan.planmanager.bean.ProjectUploadDetailBean;
import com.jinduplan.planmanager.bean.ProjectUploadSuccessBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by CZG on 2018/7.
 * 搜索过滤RecyclerView
 */

public abstract class CustomRecyclerFilterableAdapter<T> extends RecyclerView.Adapter<ViewHolder> implements Filterable {
    protected Context mContext;
    protected int mLayoutId;
    protected ArrayList<T> mDatas;
    protected ArrayList<T> othterDatas = new ArrayList<>();
    private ArrayList<T> mFilterList = new ArrayList<>();

    private OnItemClick mOnItemClick;

    public void setOnItemClickListener(OnItemClick onItemClick) {
        this.mOnItemClick = onItemClick;
    }

    public CustomRecyclerFilterableAdapter(Context context, int layoutId, ArrayList<T> datas) {
        mContext = context;
        mLayoutId = layoutId;
        mDatas = datas;
        mFilterList = datas;
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            //执行过滤操作
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                String charString = charSequence.toString();
                if (charString.isEmpty()) {
                    //没有过滤的内容，则使用源数据
                    mFilterList = mDatas;
                } else {
                    ArrayList<T> filteredList = new ArrayList<>();
                    for (T  str : mDatas) {
                        //这里根据需求，添加匹配规则
                        if(str instanceof ProjectLabour.DataBean){
                            if (((ProjectLabour.DataBean) str).getFull_name().contains(charString)) {
                                filteredList.add(str);
                            }
                        }
                        if(str instanceof ProjectUploadDetailBean.DataBean.BuildingBean){
                            if (((ProjectUploadDetailBean.DataBean.BuildingBean) str).getBuilding_name().contains(charString)) {
                                filteredList.add(str);
                            }
                        }
                        if(str instanceof ProjectUploadDetailBean.DataBean.FacadeBean){
                            if (((ProjectUploadDetailBean.DataBean.FacadeBean) str).getFacade_name().contains(charString)) {
                                filteredList.add(str);
                            }
                        }
                        if(str instanceof ProjectUploadDetailBean.DataBean.HangingBlueBean){
                            if (((ProjectUploadDetailBean.DataBean.HangingBlueBean) str).getHanging_blue_name().contains(charString)) {
                                filteredList.add(str);
                            }
                        }
                        if(str instanceof ProjectUploadSuccessBean){
                            if (((ProjectUploadSuccessBean) str).getFull_name().contains(charString)) {
                                filteredList.add(str);
                            }
                        }
                        if(str instanceof DataProjectBean.DataBean.FacadeBean){
                            if (((DataProjectBean.DataBean.FacadeBean) str).getFacade_name().contains(charString)) {
                                filteredList.add(str);
                            }
                        }
                        if(str instanceof DataProjectBean.DataBean.HangingBlueBean){
                            if (((DataProjectBean.DataBean.HangingBlueBean) str).getHanging_blue_name().contains(charString)) {
                                filteredList.add(str);
                            }
                        }

                    }

                    mFilterList = filteredList;
                }

                FilterResults filterResults = new FilterResults();
                filterResults.values = mFilterList;
                return filterResults;
            }

            //把过滤后的值返回出来
            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                mFilterList = (ArrayList<T>) filterResults.values;
                notifyDataSetChanged();
            }
        };
    }

    @Override
    public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        ViewHolder viewHolder = ViewHolder.get(mContext, null, parent, mLayoutId, -1);
        setListener(parent, viewHolder, viewType);
        return viewHolder;
    }

    protected int getPosition(RecyclerView.ViewHolder viewHolder) {
        return viewHolder.getAdapterPosition();
    }

    protected boolean isEnabled(int viewType) {
        return true;
    }


    protected void setListener(final ViewGroup parent, final ViewHolder viewHolder, int viewType) {
        if (!isEnabled(viewType)) {
            return;
        }
        viewHolder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClick != null) {
                    int position = getPosition(viewHolder);
                    mOnItemClick.onItemClick(parent, v, mFilterList.get(position), position);
                }
            }
        });


        viewHolder.getConvertView().setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnItemClick != null) {
                    int position = getPosition(viewHolder);
                    return mOnItemClick.onItemLongClick(parent, v, mFilterList.get(position), position);
                }
                return false;
            }
        });
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.updatePosition(position);
        convert(holder, mFilterList.get(position), position);
    }

    public abstract void convert(ViewHolder holder, T t, int position);

    @Override
    public int getItemCount() {
        return mFilterList.size();
    }

    public ArrayList<T> getData() {
        return mFilterList;
    }

    public ArrayList<T> getOthterData() {
        return othterDatas;
    }

    public void chooseItem(int classifyId) {
        notifyDataSetChanged();
    }

    public void setNewDatas(ArrayList<T> datas) {
        mFilterList.clear();
        mFilterList.addAll(datas);
    }


}